Automatic Deployments: CodeBuild

Getting GitHub credentials, creating an S3 bucket for build artifacts, and telling CodeBuild to pull the changes from GitHub will be demonstrated in this lesson.

Objective#

  • Automatically update our application when a change gets pushed to GitHub.

Steps#

  • Get GitHub credentials.
  • Creating S3 bucket for build artifacts.
  • CodeBuild to pull changes from GitHub.

In this section, we’re going to use CodeBuild, CodeDeploy, and CodePipeline so that our application gets updated automatically as soon as we push a change to GitHub.

GitHub access token#

We will need a GitHub access token to let CodeBuild pull changes from GitHub. To generate an access token, go to https://github.com/settings/tokens/new and click Generate new token. Give it repo and admin:repo_hook permissions, and click Generate token.

GitHub Access Token Generation
GitHub Access Token Generation

Tokens and passwords are sensitive information and should not be checked into source repositories. There are sophisticated ways to store them, but for now we’ll put our new token in a local file that we can later read into an environment variable.

terminal

Line #3: Replace <username> with your GitHub username.

Line #4: Replace <token> with your GitHub access token.

S3 bucket for build artifacts#

CodePipeline requires an S3 bucket to store artifacts built by CodeBuild. We chose to create this bucket outside of our main CloudFormation template because CloudFormation is unable to delete S3 buckets unless they’re empty. This limitation becomes very inconvenient during development, because you would have to delete the S3 bucket manually every time you tear down your CloudFormation stack. Therefore, we like to put resources such as these in a separate CloudFormation template called setup.yml.

setup.yml

Now let’s edit our deploy-infra.sh script to define the S3 bucket name for our CodePipeline.

terminal

Line #1: This is a way to programmatically get the AWS account ID from the AWS CLI.

Line #3: S3 bucket names must be globally unique across all AWS customers. Adding our account ID to the bucket name helps prevent name conflicts.

Then we need to deploy setup.yml from our deploy-infra.sh script, just before we deploy main.yml.

deploy-infra.sh

Start and stop scripts#

Next, we need to create a couple of simple scripts to tell CodeDeploy how to start and stop our application.

start-service.sh

Line #2: Makes sure any user-specific software that we’ve installed (e.g., npm via nvm) is available.

Line #3: Changes into the working directory in which our application expects to be run.

Line #4: Runs the start script we put in package.json.

stop-service.sh

The build specification#

Next, we need to tell CodeBuild how to build our application. To do this, CodeBuild has a specification, which we use in a file named buildspec.yml.

buildspec.yml

The deployment specification#

Now, we need to tell CodeDeploy what to do with the build artifacts created by CodeBuild. To do this, CodeDeploy also has a specification, which we use in a file named appspec.yml.

appspec.yml

At this point, let’s commit what we have so far to GitHub.

terminal

In the next lesson, we are going to install CodeDeploy agent on our EC2 instance.

Infrastructure as Code: Deploy CloudFormation Stack
Automatic Deployments: Install CodeDeploy Agent on EC2
Mark as Completed
Report an Issue